//
//  ViewController.m
//  TPPickerDemo
//
//  Created by 谭鹏 on 2017/8/4.
//  Copyright © 2017年 谭鹏. All rights reserved.
//

#import "ViewController.h"
#import <Masonry.h>
#import "TPPicker_general.h"
#import "TPPicker_date.h"

@interface ViewController ()<UITableViewDataSource ,UITableViewDelegate,TPPicker_generalDataSource>
@property NSArray * pcikerTypes;
    @property TPPicker_general * generalPicker;
        @property UITableView * generalTableView_1,* generalTableView_2;
        @property NSArray*  generalPickerDataSource;
    @property TPPicker_date * datePicker;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView * tableView = [UITableView new];
    tableView.dataSource = self;
    tableView.delegate = self;
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.edges.mas_equalTo(self.view);
    }];
    
    _pcikerTypes = @[@"generalPicker",@"datePicker"];
    
    _generalPicker = [TPPicker_general new];
        _generalPicker.dataSource = self;
        _generalTableView_1 = [UITableView new];
        _generalTableView_2 = [UITableView new];
        
    
        _generalPickerDataSource = @[
                                     @{@"name":@"express",@"value":@[@"圆通",@"中通",@"顺丰",@"邮政"]},
                                     @{@"name":@"number",@"value":@[@"1",@"2",@"3",@"4"]}
                                     ];
        [_generalPicker setTableViews:@[_generalTableView_1,_generalTableView_2] height:250];
        [_generalPicker reloadDataSource];
    
    
    _datePicker = [TPPicker_date new];
    
    [_datePicker setDateType:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay];
    [_datePicker updateSelectedDate:[NSDate new]];
    
    [_datePicker.pickerView showAnyViewIsRecursive:NO];
    
    
    
//        UIView * topLineView = [_datePicker.pickerView valueForKey:@"topLineView"];
//        UIView * bottomLineView = [_datePicker.pickerView valueForKey:@"bottomLineView"];
//        [topLineView setHidden:YES];
//        [bottomLineView setHidden:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pcikerTypes.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
      cell =  [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.textLabel.text = _pcikerTypes[indexPath.row];
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        [_generalPicker show];
    }else{
        [_datePicker show];
    }
    
}
#pragma mark - TPPicker datasource
//-(CGFloat)picker:(TPPicker_general *)pciker widthFortableView:(UITableView *)tableView{
//    return  150;
//}
-(UITableViewCell *)picker:(TPPicker_general *)pciker tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    if (tableView == _generalTableView_1) {
        cell.textLabel.text = _generalPickerDataSource[indexPath.row][@"name"];
    }else if (tableView == _generalTableView_2){
        cell.textLabel.text = _generalPickerDataSource[0][@"value"][indexPath.row];
    }else{
        
    }
    return cell;
}
- (NSInteger)picker:(TPPicker_general *)picker tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (tableView == _generalTableView_1) {
        return _generalPickerDataSource.count;
    }else if (tableView == _generalTableView_2){
        return [_generalPickerDataSource[0][@"value"] count];
    }else{
        return 0;
    }
}
@end
